var $ = require("../../include/$");
var tablename = "message";
module.exports = {
    post: function(req, res) {
        // console.log(req.body);
        var nodemailer = require("nodemailer");
        var transport = nodemailer.createTransport("SMTP", {
            host: "smtp.163.com", // 主机
            secureConnection: true, // 使用 SSL
            port: 994, // SMTP 端口
            auth: {
                user: "dotnar@163.com",
                pass: "dianna123456"
            }
        });
        transport.sendMail({
            from: "dotnar@163.com",
            to: req.body.email,
            subject: "点纳系统回复：" + req.body.replytitle,
            generateTextFromHTML: true,
            html: "<p>" + req.body.replytext + "</p>"
        }, function(error, response) {
            if (error) {
                console.log(error);
            } else {
                // console.log("Message sent: " + response.message);
                var replyitem = {
                    "msgid": req.body.msgid,
                    "replytitle": req.body.replytitle,
                    "replytext": req.body.replytext
                }
                req.models.reply.create($.extend("", replyitem), function(err, result) {
                    if (err) {
                        // console.log(err);
                        res.send(500, err);
                    } else {
                        res.send(true);
                    }
                });
                var msgid = req.body.msgid;
                req.models[tablename].get(msgid, function(err, result) {
                    if (err) {
                        res.send(500, err);
                    } else {
                        for (msgid in result) {
                            result['type'] = 3;
                        };
                        result.save(function(err) {
                            if (err) {
                                res.send(500, err);
                            } else {
                                res.send(result);
                            }
                        });
                    }
                });
            }
            transport.close();
        });



    },
}